package com.mf.utils.calculate;

import com.mf.entity.Deal;
import com.mf.entity.StockPool;
import com.mf.entity.WinRate;
import com.mf.vo.Calculate1Dto;
import com.mf.vo.MACD.MACDVo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lmf
 * @Create: 2024/5/2 21:02
 * Description:  计算方案1
 * 1: 获取cci5日向上趋势
 * 2: macd 5日趋势向上
 * 3: 5 日线大于 10日线
 */
public class Calculate1 {
    // 新版计算方法
    public static Calculate1Dto findIncreasingIndices2(MACDVo macdVo, int count) {
        List<Deal> dealList =  new ArrayList<>();
        List<StockPool> stockPools = new ArrayList<>();
        double cost = 10000;

        for (int i = count; i < macdVo.getCci().size(); i++) {
            final boolean c = printGrowingSequenceStartIndices(macdVo.getCci(), i, count);

            final boolean macd = printGrowingSequenceStartIndices(macdVo.getMacd(), i, count);
            if (c && macd && macdVo.getMa5().get(i) > macdVo.getMa10().get(i)) {
                final String[] date = macdVo.getDate();
                final double[][] k = macdVo.getK();
                final Deal deal = new Deal();
                i += 3;
                if (i + 4 <= k.length) {
                    final double v1 = k[i][1];
                    final double d1 = isDifferenceAtLeastOnePercent(k[i + 2][3], v1);
                    final double d2 = isDifferenceAtLeastOnePercent(k[i + 3][3], v1);
                    final double d3 = isDifferenceAtLeastOnePercent(k[i + 4][3], v1);
                    double percent = 0.01;

                    deal.setDm(macdVo.getDm());
                    deal.setSale(0);
                    deal.setDate(date[i]);
                    deal.setBuyingPrice(BigDecimal.valueOf(v1));
                    deal.setPurchaseAmount(BigDecimal.valueOf(cost));

                    if(d1 >=  percent || d2 >=  percent || d3 >=  percent){
                        deal.setStatus(1);
                        cost = cost + cost * 0.01;
                        if(d1>percent){
                            deal.setSale(1);
                            deal.setSellingPrice(BigDecimal.valueOf(k[i + 2][3]));
                        }else if(d2>percent){
                            deal.setSale(2);
                            deal.setSellingPrice(BigDecimal.valueOf(k[i + 3][3]));
                        }else if(d3>percent){
                            deal.setSale(3);
                            deal.setSellingPrice(BigDecimal.valueOf(k[i + 4][3]));
                        }
                        deal.setSellingAmount(BigDecimal.valueOf(cost));
                    }else {
                        deal.setStatus(2);
                        final double v = isDifferenceAtLeastOnePercent(k[i + 4][1], v1);
                        cost = cost - cost * v;
                        deal.setSellingAmount(BigDecimal.valueOf(cost));
                    }
                }else{
                    // 实时的推荐
                    final StockPool stockPool = new StockPool();
                    stockPool.setDm(macdVo.getDm());
                    stockPool.setDate(date[i]);
                    stockPools.add(stockPool);
                }
                dealList.add(deal);
            }
        }
        final Calculate1Dto calculate1Dto = new Calculate1Dto();
        calculate1Dto.setDealList(dealList);
        calculate1Dto.setStockPoolList(stockPools);
        return  calculate1Dto;
    }

    public static boolean compareArray(MACDVo macdVo, int i,int count){
        // k[0] 开盘加   k[1] 收盘价  k[2] 最低价  k[3] 最高价
        // 当前的k线
        final double[] k = macdVo.getK()[i];
        int num = 0;
        System.out.println(macdVo.getMa5().get(i)+"macdVo.getMa5().get(i)");
        System.out.println(k[1]+"k[1]");
        if(macdVo.getMa5().get(i) <  k[1]){
            for (int j = i; j > i - count; j--) {
                System.out.println(macdVo.getMa5().get(j)+"===========死=================="+macdVo.getMa5().get(i));
                if(macdVo.getMa5().get(j) >  macdVo.getMa5().get(i)){
                    System.out.println("=================?????????????????????????????/");
                    num++;
                }
            }
        }

        return num >= count-1;
    }
    public static WinRate findIncreasingIndices(MACDVo macdVo, int count) {
        int success = 0;
        int s1 = 0;
        int s2 = 0;
        int s3 = 0;
        int error = 0;
        BigDecimal cost = new BigDecimal("1000.0");

        final WinRate winRate = new WinRate();
        int flag = 0;
        for (int i = count; i < macdVo.getCci().size(); i++) {
//            final boolean c = printGrowingSequenceStartIndices(macdVo.getCci(), i, count);
//            final boolean macd = printGrowingSequenceStartIndices(macdVo.getMacd(), i, count);

            final double[][] k = macdVo.getK();
            final String[] date = macdVo.getDate();
            // 进入买入状态
            if(flag == 1){
                // 这时开始买入
                final double v1 = k[i][0];
                System.out.println(v1+"v1");
                System.out.println(isDifferenceAtLeastOnePercent(k[i][1],k[i+1][1]));
                System.out.println(k[i][1]+"=============");
                System.out.println(k[i+1][1]+"k[i-1][1]");
                if(isDifferenceAtLeastOnePercent(k[i][1],k[i+1][1]) < -0.02){
                    System.out.println("开始卖出"+date[i+1]);
                    flag = 0;
                }
            }
            // 5日线小于当前收盘价 并且前5天 5日线5日线大于当前的5日线
            if(compareArray(macdVo,i,count) && flag != 1){
                System.out.println("==========买入日期:"+date[i+1]+"+=========价格");
                flag = 1;
            }



//            if (macdVo.getMa5().get(i) <  macdVo.getK()[i][1] && ) {
//                final String[] date = macdVo.getDate();
//                // k[0] 开盘加   k[1] 收盘价  k[2] 最低价  k[3] 最高价
//                final double[][] k = macdVo.getK();
//                final double v1 = k[i+1][0];
//
//
//
//
//
//                // &&  > macdVo.getMa10().get(i)
//                System.out.println(macdVo.getMa5().get(i)+"==============================");
//                System.out.println(macdVo.getMa10().get(i)+"==============================");
//
////                i += 3;
////                if (i + 4 < k.length) {
////                    final double v1 = k[i+1][0];
////                    final double d1 = isDifferenceAtLeastOnePercent(k[i + 2][3], v1);
////                    final double d2 = isDifferenceAtLeastOnePercent( k[i + 3][3], v1);
////                    final double d3 = isDifferenceAtLeastOnePercent(k[i + 4][3], v1);
////                    double percent = 0.01;
////
////                    if(d1 >=  percent || d2 >=  percent || d3 >=  percent){
////                        System.out.println(date[i+1] + "成功++++++++++++++++++++++++++++++++++++++++++++++++++++买入价格:" + k[i+1][0]);
////                        System.out.println(date[i+2] + "后一天最高价"+k[i + 2][3]);
////                        System.out.println(date[i+3] + "后2天最高价"+k[i + 3][3]);
////                        System.out.println(date[i+4] + "后3天最高价"+k[i + 4][3]);
////                        final BigDecimal multiply = cost.multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP);
////                        cost = cost.add(multiply);
////
////                        System.out.println("成===================结束=========================");
////
////
////
////                        if(d1>percent){
//////                            System.out.println("第一天卖出");
////                            s1++;
////                        }else if(d2>percent){
//////                            System.out.println("第二天卖出");
////                            s2++;
////                        }else if(d3>percent){
//////                            System.out.println("第三天卖出");
////                            s3++;
////                        }
////
////                        winRate.setSuccessDate(date[i]);
////                        success++;
////                    }else {
//////                        System.out.println("失败开始===================================================");
////                        final double v = isDifferenceAtLeastOnePercent(k[i + 4][1], v1);
////                        // 直接使用double进行计算
////                        final BigDecimal multiply2 = cost.multiply(new BigDecimal(v)).setScale(2, RoundingMode.HALF_UP);
////                        cost =  cost.add(multiply2);
////                        error++;
////                        winRate.setFailDate(winRate.getFailDate()+","+date[i]);
//////                        System.out.println("失败结束===================================================");
////
////                    }
////                }
//            }

        }
        winRate.setSuccess(success);
        winRate.setError(error);
        winRate.setDm(macdVo.getDm());
        winRate.setCost(cost);
        winRate.setS1(s1);
        winRate.setS2(s2);
        winRate.setS3(s3);
        return  winRate;
    }

    // 总数据   i   趋势关联数量
    public static boolean printGrowingSequenceStartIndices(List<Double> doubles, int number,int count){
        int num = 0;
        for (int i = number; i > number - count; i--) {
            if (doubles.get(i-1) <= doubles.get(i)) {
                num++;
            }
        }
        return num >= count;
    }

    public static boolean printGrowingSequenceStartIndices(double[] doubles, int number,int count){
        int num = 0;
        for (int i = number; i > number - count; i--) {
            if (doubles[i-1] <= doubles[i]) {
                num++;
            }
        }
        return num >= count;
    }

    public static double isDifferenceAtLeastOnePercent(double  a, double  b) {
        final BigDecimal aa = new BigDecimal(a);
        final BigDecimal bb = new BigDecimal(b);
        // 计算相对差值
        return  ((aa.subtract(bb)).divide(bb, 2, RoundingMode.HALF_UP)).doubleValue();
    }


}
